package com.example.plcopne.ui

import android.os.Bundle
import android.util.Log
import android.view.View
import com.example.plcopne.LogBean
import com.example.plcopne.R
import com.example.plcopne.adapter.LogListAdapter
import com.example.plcopne.adapter.PosiAdapter
import com.example.plcopne.bean.PosiBean
import com.example.plcopne.utils.PlcUtils
import com.example.plcopne.utils.RxUtils
import com.lezhu.plcsdklibrary.Constants
import com.lezhu.plcsdklibrary.OperatListUtls
import com.lezhu.plcsdklibrary.PlcSDK
import com.lezhu.plcsdklibrary.callback.DoorListener
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import kotlinx.android.synthetic.main.activity_main.*
import java.util.concurrent.TimeUnit

class PLcActivity : BaseActivity(), View.OnClickListener, DoorListener {


    lateinit var logListAdapter: LogListAdapter
    lateinit var posiAdapter: PosiAdapter

    var polist = mutableListOf<PosiBean>()
    override fun getContentView(): Int {
        return R.layout.activity_main
    }

    override fun init(savedInstanceState: Bundle?) {
        btn_send.setOnClickListener(this)
        btn_empty.setOnClickListener(this)
        logListAdapter = LogListAdapter(null)
        settitle("柜门测试")
        recy.adapter = logListAdapter
        posiAdapter = PosiAdapter()
        rl_posi.adapter = posiAdapter
        val toList = Constants.doorList.toList()
        polist.clear()
        toList.forEach {
            polist.add(PosiBean(it, 5))
        }




        posiAdapter.setNewInstance(polist)
        posiAdapter.setOnItemClickListener { _, _, position ->
            PlcSDK.getInstance().OpenDoor(position + 1)
//            PlcUtils.getInstance().OpenDoor(position + 1)
        }

        PlcSDK.getInstance().init()

        OperatListUtls.getInstance().setListener { log: String?, type: Int ->
            val logBean = LogBean()
            logBean.type = type
            logBean.log = log
            logListAdapter.addData(logBean)
            recy.smoothScrollToPosition(logListAdapter.data.size)
        }


        //todo 注册柜门状态监听
//        PlcSDK.getInstance().addDoorListener(this)
        PlcUtils.getInstance().addDoorListener(this)

        btn_open_all.setOnClickListener {
           val toList1 = (0..29).toList()

            val listObservable: Observable<Int> = Observable.fromIterable(toList1)
            val timerObservable = Observable.interval(1000, TimeUnit.MILLISECONDS)
            Observable.zip(listObservable, timerObservable, { t1, t2 ->
                return@zip t1
            })
                .subscribeOn(AndroidSchedulers.mainThread())
                .doOnNext {
                    runOnUiThread {
                        PlcUtils.getInstance().OpenDoor(it + 1)
                    }
                }
                .`as`(RxUtils.bindLifecycle(this))
                .subscribe()


        }
    }


    override fun onClick(v: View?) {
        when (v!!.id) {
            R.id.btn_send -> {
                SendCMD()
            }

            R.id.btn_empty -> {
                logListAdapter.setNewInstance(null)
            }

        }


    }

    /**
     * 发送命令
     */
    fun SendCMD() {
        PlcUtils.getInstance().OpenDoor(pos + 1)
    }


    private var pos = 0


    override fun doorOpen(pos: Int) {
        Log.e("tag", "doorOpen: $pos")
        val posiBean = posiAdapter.data[pos - 1]
        posiBean.state = 4
        posiAdapter.setData(pos - 1, posiBean)

    }

    override fun doorClose(pos: Int) {
        Log.e("tag", "doorClose: $pos")
        val posiBean = posiAdapter.data[pos - 1]
        posiBean.state = 5
        posiAdapter.setData(pos - 1, posiBean)
    }

}